package com.jiai.cloud.controller;

<<<<<<< Updated upstream
import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TApplication;
=======
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.tools.RestResultGenerator;
>>>>>>> Stashed changes
import com.jiai.cloud.entity.TOrder;
import com.jiai.cloud.feign.OrderAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with j360-cloud-all -> me.j360.cloud.eurekaclient.controller.
 * User: min_xu
 * Date: 2015/10/9
 * Time: 10:53
 * 说明：
 */

@RestController
@RequestMapping("/api/v1/order")
public class OrderController {

    @Autowired
    OrderAPI orderAPI;


    /**
     * 直接调用feign，feign会去调用eurekaService
     * */
<<<<<<< Updated upstream
    //查找所有的
    @RequestMapping(value = "/findByAll/{page}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object findOrder(@PathVariable("page") Integer page1) throws ServiceException {
        try {
        Page<TApplication> page = new Page<>(page1, 20);
        orderAPI.selectPage(page1);
        return RestResultGenerator.genSuccessResult(new CustomPage(page));
    } catch (Exception e) {
        throw new ServiceException("chaxun", e.getMessage());
    }
}
    //根据订单查找
    @RequestMapping(value = "/findByid/{orderNo}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object findByid(@PathVariable("orderNo") String orderNo) throws ServiceException {
        try {

            return RestResultGenerator.genSuccessResult(orderAPI.selectById(orderNo));
        } catch (Exception e) {
            throw new ServiceException("chaxun", e.getMessage());
        }
    }
    @RequestMapping(value = "/del/{orderNo}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object deleteId(@PathVariable("orderNo") String orderNo) throws ServiceException {
        try{
            orderAPI.deleteId(orderNo);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //   保存和 更新
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit(@RequestBody TOrder tOrder)throws ServiceException{
        try {
            orderAPI.insertOrUpdate(tOrder);

            return RestResultGenerator.genSuccessResult() ;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }
=======
>>>>>>> Stashed changes

    //增
    @RequestMapping(value="/addOrder", method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object saveOrder(@RequestBody TOrder tOrder){
        orderAPI.addOrder(tOrder);
        return RestResultGenerator.genSuccessResult("");
    }
    //删
    @RequestMapping(value = "/del/{orderNo}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object deleteNo(@PathVariable("orderNo") String orderNo) throws ServiceException {
        try{
            orderAPI.delete(orderNo);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    //查找所有的
    @RequestMapping(value = "/findByAll",method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object findOrder() throws ServiceException {
        try {

            return RestResultGenerator.genSuccessResult(orderAPI.findByAll());
        } catch (Exception e) {
            throw new ServiceException("", e.getMessage());
        }
    }

    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object updateOrInsert(@RequestBody TOrder tOrder)throws ServiceException{
        try {
            orderAPI.edit(tOrder);
            return RestResultGenerator.genSuccessResult() ;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }
}
